<?php

declare(strict_types=1);

namespace App\Model;

use App\Helpers\AliyunUtil;
use App\Helpers\DateUtil;
use Hyperf\DbConnection\Model\Model;

/**
 *  App\Models\Banner
 *  Banner模型
 */
class Banner extends Model
{
    protected $table = 'banner';

    //跳转H5链接
    const JUMP_TYPE_LINK = 1;
    //跳转用户详情
    const JUMP_TYPE_USER = 2;

    const JUMP_TYPE_WX = 3;

    /**
     * 拉取列表
     */
    static function getList($platform = 1, $gender = -1, $appChannel = '')
    {
        $now = DateUtil::format();
        $where = [
            ['flag', '=', 1],
            ['begin_at', '<=', $now],
            ['end_at', '>=', $now],
            ['jump_type','<>', self::JUMP_TYPE_WX]
        ];
        return self::where($where)
            ->whereIn('channel', [0, $platform])
            ->where('app_channel', 'like', "%{$appChannel}%")
            ->whereIn('gender', [-1, $gender])->orWhere(function ($query) use ($where, $platform, $gender) {
                $query->where($where)
                    ->whereIn('channel', [0, $platform])
                    ->where('app_channel', '')
                    ->whereIn('gender', [-1, $gender]);
            })->orderBy('id', 'desc')->get()->toArray();
    }

    /**
     * @param int $platform
     * @param int $gender
     * @param string $appChannel
     * @return array
     */
    public static function getListV1(int $platform = 1, int $gender = -1, string $appChannel = ''): array
    {
        $now = DateUtil::format();
        $where = [
            ['flag', '=', 1],
            ['begin_at', '<=', $now],
            ['end_at', '>=', $now],
        ];
        return self::where($where)
            ->whereIn('channel', [0, $platform])
            ->where('app_channel', 'like', "%{$appChannel}%")
            ->whereIn('gender', [-1, $gender])->orWhere(function ($query) use ($where, $platform, $gender) {
                $query->where($where)
                    ->whereIn('channel', [0, $platform])
                    ->where('app_channel', '')
                    ->whereIn('gender', [-1, $gender]);
            })->orderBy('id', 'desc')->get()->toArray();
    }

    /**
     * 封装数据
     * @param null $banner
     * @param null $profile
     * @return array
     */
    static function packBanner($banner = null, $profile = null): array
    {
        $jumpInfo = $banner['jump_info'] ? json_decode($banner['jump_info'], true) : null;
        $jumpInfo && $jumpInfo['title'] = $banner['title'];
        if (isset($jumpInfo['link']) && strpos($jumpInfo['link'], 'invit') !== false && $profile) {
            $jumpInfo['link'] .= "?invitecode={$profile->user_profile_id}";
        }

        $wxInfo = $banner['wx_info'] ? json_decode($banner['wx_info'], true) : [
            'appid' => '',
            'original_id' => '',
            'link' => ''
        ];
        $ret = [
            'poster' => strpos($banner['poster'], 'http') === false ? AliyunUtil::IMG_HOST_V2 . $banner['poster'] : $banner['poster'],
            'title' => $banner['title'],
            'jump_type' => $banner['jump_type'],
            'jump_info' => $jumpInfo,
            'wx_info' => $wxInfo
        ];
        if ($banner['jump_type'] == self::JUMP_TYPE_USER) {
            $profile = UserProfile::getInfoByProfileId($jumpInfo['link']);
            $profile && $ret['user_id'] = $profile->user_id;
        }
        return $ret;
    }
}
